/**
 * 
 */
package com.mvc.service.healthmanager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.huangqj.consant.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mvc.dao.BaseDaoI;
import com.mvc.dao.mapper.WeightHeightDao;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.base.BaseData;
import com.mvc.model.healthmanager.archive.WeightHeight;
import com.mvc.model.mb.archive.BaseInfo;
import com.mvc.model.personHealth.UserWeightHeight;
import com.mvc.service.healthmanager.WeightHeightServiceI;
import com.mvc.utils.CreatHighChartJson;
import com.mvc.utils.StringUtil;

/**
 * @author qingjie
 *
 */
@Service
public class WeightHeightServerImpl implements WeightHeightServiceI {
	private String titleName = "体重身高数据走势";
	private String unit = "";
	private String yAxixname = "数据";
	
	@Autowired
	private WeightHeightDao wHeightDao;
	@Autowired
	private BaseDaoI<WeightHeight> whDaoI;
	@Autowired
	private BaseDaoI<BaseInfo> bDaoI;
	/**
	 * 根据个人id,获取最新体重记录
	 * @author qingjie
	 * @param personId
	 * @return
	 * Date:2015-11-15
	 */
	@Override
	public WeightHeight getNewWeightHeight(WeightHeight wh) {
		Map<String, Object> params = StringUtils.beanToMap(wh);
		List<WeightHeight> list = whDaoI.find(StringUtil.getHql("WeightHeight", params, "desc"), params);
		return list.size()>0?list.get(0):new WeightHeight();
	}
	@Override
	public List<WeightHeight> getWeightHeight(WeightHeight wh) {
		return wHeightDao.getWeightHeight(wh);
	}
	@Override
	public String getJsonData(String personId) {
		List<WeightHeight> list = wHeightDao.getJsonData(personId);
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>();
		if(!list.isEmpty()){
			Map<String, Object> w_m = new HashMap<String, Object>();
			w_m.put("name", "体重(kg)");
			List<Double> w_l = new ArrayList<Double>();
			Map<String, Object> h_m = new HashMap<String, Object>();
			h_m.put("name", "身高(cm)");
			List<Double> h_l = new ArrayList<Double>();
			Map<String, Object> y_m = new HashMap<String, Object>();
			y_m.put("name", "腰围(cm)");
			List<Double> y_l = new ArrayList<Double>();
			Map<String, Object> b_m = new HashMap<String, Object>();
			b_m.put("name", "体质(bmi)");
			List<Double> b_l = new ArrayList<Double>();
			
			for(int i=0; i<list.size(); i++){
				WeightHeight wHeight = list.get(i);
				xAxis[i] = GlobalConstant.SDF.format(wHeight.getCreatDate());
				w_l.add(i,wHeight.getWeight());
				h_l.add(i, wHeight.getHeight());
				y_l.add(i, wHeight.getWaist());
				b_l.add(i, wHeight.getBmi());
			}
			w_m.put("data", w_l);h_m.put("data", h_l);y_m.put("data", y_l);b_m.put("data", b_l);
			map.put("weight", w_m);map.put("height", h_m);map.put("waist", y_m);map.put("bmi", b_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}
	@Override
	public List<WeightHeight> findWeightHeight(Map<String, Object> params,String order) {
		return whDaoI.find(StringUtil.getHql("WeightHeight", params, order),params);
	}

	/**
	 * @author Roy
	 * 根据不同条件查询体重表
	 * @param param
	 * @return
	 */
	@Override
	public List<WeightHeight> finWeightHeights(Map<String, Object> params,String order) {
		List<WeightHeight> weightHeight = whDaoI.find(StringUtil.getHql("WeightHeight", params,order), params);
		
		if(weightHeight.size()<=0){
			WeightHeight w = new WeightHeight();
			List<BaseInfo> bi = bDaoI.find(StringUtil.getHql("BaseInfo", params, order), params);
			BaseData bd = new BaseData();
			if(bi!=null){
				bd = JSON.parseObject(bi.get(0).getMbInfo(),BaseData.class);
			}
			w.setWeight(bd.getWeight());
			w.setHeight(bd.getHeight());
			w.setWaist(bd.getWaist());
			w.setBmi(bd.getBmi());
			weightHeight.add(w);
		}
		return weightHeight;
	}
	@Override
	public UserWeightHeight getUserYhWeightById(UserWeightHeight uWeight) {
		return wHeightDao.getUserYhWeightById(uWeight);
	}
	
	/**
	 * 获取体重变化json
	 * @author qingjie
	 * @param personId
	 * @return
	 */
	@Override
	public String getBmiJson(String personId) {
		titleName = "体重指数变化趋势图";
		unit = "kg/m2";
		yAxixname = "kg/m2";
		List<WeightHeight> list = whDaoI.find("from WeightHeight b where b.personId = '"+personId+"' order by b.count asc");
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>(); 
		if(!list.isEmpty()){
			Map<String, Object> bmi_m = new HashMap<String, Object>(); 
			bmi_m.put("name", "体重指数");
			List<Double> bmi_l = new ArrayList<Double>();
			
			
			for(int i=0; i<list.size(); i++){
				WeightHeight wh = list.get(i);
				xAxis[i] = GlobalConstant.SDF.format(wh.getCreatDate());
				bmi_l.add(i, wh.getBmi());
			}
			bmi_m.put("data", bmi_l);
			map.put("fbg", bmi_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}
	/**
	 * 获取腰围变化json
	 * @author qingjie
	 * @param personId
	 * @return
	 */
	@Override
	public String getWaistJson(String personId) {
		String titleName = "腰围变化趋势图";
		String unit = "cm";
		String yAxixname = "cm";
		List<WeightHeight> list = whDaoI.find("from WeightHeight b where b.personId = '"+personId+"' order by b.count asc");
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>(); 
		if(!list.isEmpty()){
			Map<String, Object> waist_m = new HashMap<String, Object>(); 
			waist_m.put("name", "腰围");
			List<Double> waist_l = new ArrayList<Double>();
			
			
			for(int i=0; i<list.size(); i++){
				WeightHeight wh = list.get(i);
				xAxis[i] = GlobalConstant.SDF.format(wh.getCreatDate());
				waist_l.add(i, wh.getWaist());
			}
			waist_m.put("data", waist_l);
			map.put("fbg", waist_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}

}
